package com.cakebackoffice;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.cakebackoffice.Model.Order;
import com.cakebackoffice.View.PriorityButton;

import java.util.List;

public class ListViewOrderAdapter extends ArrayAdapter<Order> {

    int resource;

    public ListViewOrderAdapter(Context context, int textViewResourceId, List<Order> objects) {
        super(context, textViewResourceId, objects);
        this.resource = textViewResourceId;
    }

    // On override ArrayAdapter
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LinearLayout orderView;
        // Récuperer l'order courant
        Order order = getItem(position);

        // Inflate the catalog_detail_product
        if (convertView == null) {
            orderView = new LinearLayout(getContext());
            String inflater = Context.LAYOUT_INFLATER_SERVICE;
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(inflater);
            vi.inflate(resource, orderView, true);
        } else {
            orderView = (LinearLayout) convertView;
        }

        // Récupérer les View des orderView
        TextView clientName = (TextView) orderView.findViewById(R.id.textViewClientName);
        TextView totalProducts = (TextView) orderView.findViewById(R.id.textViewTotalProducts);
        TextView clientAddress = (TextView) orderView.findViewById(R.id.textViewClientAddress);
        TextView phoneNumber = (TextView) orderView.findViewById(R.id.textViewPhoneNumber);
        TextView deliveryType = (TextView) orderView.findViewById(R.id.textViewDeliveryType);
        TextView deliveryDate = (TextView) orderView.findViewById(R.id.textViewDeliveryDate);
        TextView totalTTC = (TextView) orderView.findViewById(R.id.textViewTotalTTC);
        PriorityButton priority = (PriorityButton) orderView.findViewById(R.id.button);
        try {
            // les rempliiiir !!
            clientName.setText(order.getClient().getFirstName() + " " + order.getClient().getName());
            totalProducts.setText(new Integer(order.getUnitarySaleList().size()).toString());            // Faut mettre des Strings !
            clientAddress.setText(order.getAddress().toString());
            phoneNumber.setText(order.getClient().getPhoneNumber());
            if (order.getDeliveryType() != null)                                  //Soit tu check qu'elles soient pas nul soit tu rebalance une excepetion pour exiger quelles ne soient pas nulle ...
                deliveryType.setText(order.getDeliveryType().toString());
            if (order.getDeliveryDate() != null && order.getOrderDate() != null)
                deliveryDate.setText("Commandée à " + order.getOrderDate().toLocaleString() + " Livraison estimée pour " + order.getDeliveryDate().toLocaleString());
            totalTTC.setText(order.getTotalAmountTTC().toString());

            priority.setColor(order.getPriority());
        } catch (Exception e) {

        }

        return (orderView);


    }


}
